<?php

    class Settings_model extends MY_Model
    {
        function __construct()
        {
            parent::__construct();
            $this->table = 'settings';
        }

        function default_thumbnails()
        {

            // loop each default size from config
            foreach(config_item('default_thumbnails') as $size):

                $create_default = TRUE;

                // get thumbanils from db settings table
                $list_thumbnails = $this->list_thumbnails();

                if(count($list_thumbnails)>0):

                    foreach($list_thumbnails as $thumb_size){
                        $dims = unserialize($thumb_size['user_data']);

                        if($dims['w']==$size['w'] && $dims['h']==$size['h'])
                            $create_default = FALSE;

                    }

                    endif;

                // create default
                if($create_default):

                    $data = array(
                        'name' => 'thumbnail_size',
                        'user_data' => serialize(array('w' => $size['w'], 'h' =>$size['h'])),
                        'description' => 'Default thumbnail <b>'.$size['w'].'x'.$size['h'].'<b>'
                    );

                    $this->insert($data);

                    endif;

                endforeach;
        }

        function list_thumbnails($get_sizes=FALSE)
        {
            $this->db->where('name','thumbnail_size');
            $this->db->order_by('ID',"DESC");
            $res = $this->db->get($this->table)->result_array();
            if ($get_sizes) $res = $this->get_sizes($res);
            return $res;
        }

        function get_sizes($thumbnails)
        {
            $sizes = array();
            foreach ($thumbnails as $thumbnail) :

                array_push ($sizes, unserialize($thumbnail['user_data']));

                endforeach;

            return $sizes;
        }


    }

?>
